//
//  LendViewController.m
//  ZYZB
//
//  Created by ENERGY on 2018/6/28.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "LendViewController.h"
#import "LendTableViewCell.h"
#import "ClassifyChooseView.h"
#import "SiftDetailViewController.h"
#import "CloseCourseView.h"
#import "StatusChooseView.h"
#import "TimeChooseView.h"
#import "RateChooseView.h"
#import "NoPlatinumMemberView.h"
#define sliderLabelW 44/WIDTH_6_SCALE
#define btnW (SCREEN_WIDTH - 30/WIDTH_6_SCALE)/3
#define btnX (btnW - sliderLabelW)/2
#define sectionH 37/WIDTH_6_SCALE
#define ifShutCourse @"ifShutCourse"
@interface LendViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UIView * navView;//上面按钮背景
@property (nonatomic, strong)UIButton * platinumBtn;//福乐白金
@property (nonatomic, strong)UIButton * interestBtn;//福乐月息
@property (nonatomic, strong)UIButton * siftBtn;//福乐精选
@property (nonatomic, strong)UILabel * sliderLabel;//下面滑动条
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIView * viewBar;//导航栏下类别选择背景框
@property (nonatomic, strong)UIView * headerView;//tableView头部
@property (nonatomic, strong)ClassifyChooseView * classifyChooseView;//下拉菜单
@property (nonatomic, strong)StatusChooseView * statusChooseView;//下拉菜单
@property (nonatomic, strong)TimeChooseView * timeChooseView;//下拉菜单
@property (nonatomic, strong)RateChooseView * rateChooseView;//下拉菜单
@property (nonatomic, strong)UIButton * classifyBtn;//分类bar第一个按钮
@property (nonatomic, strong)UIButton * statusBtn;//分类bar第二个按钮
@property (nonatomic, strong)UIButton * timeBtn;//分类bar第三个按钮
@property (nonatomic, strong)UIButton * rateBtn;//分类bar第四个按钮
@property (nonatomic, strong)NSString * bigtitle;//一级标题
@property (nonatomic, strong)NSMutableArray * classifyCellArr;//分类view数组
@property (nonatomic, strong)NSMutableArray * statusCellArr;//状态view数组
@property (nonatomic, strong)NSMutableArray * timeCellArr;//期限view数组
@property (nonatomic, strong)NSMutableArray * rateCellArr;//借率view数组
@property (nonatomic, strong)NSString * classifyContentStr;//分类具体内容
@property (nonatomic, strong)NSString * statusContentStr;//状态具体内容
@property (nonatomic, strong)NSString * timeContentStr;//期限具体内容
@property (nonatomic, strong)NSString * rateContentStr;//借率具体内容
@property (nonatomic, strong)NSMutableArray * cellDataArr;
@property (nonatomic, assign)int currentPage;//当前页数
@end

@implementation LendViewController(networking)
//分类栏
- (void)classifyMenuList{
    [self showprogressHUD];
    NSString * timeStr = [self currentTimeStr];
    //str
    NSDictionary * dic = @{
                           @"userName":[UserModel sharedInstanced].mobile,
                           @"bigtitle":self.bigtitle,
                           @"num":@"0",
                           @"t":timeStr
                           };
    //sign
    NSDictionary * signDic = @{
                               @"userName":[UserModel sharedInstanced].mobile,
                               @"bigtitle":self.bigtitle,
                               @"num":@"0",
                               @"t":timeStr,
                               @"merchantNo":@"80d709a97af1b8ab"
                               };
    SortMD5Code * md5Code = [[SortMD5Code alloc] init];
    NSString * jsonStr = [md5Code getJsonWithStrDic:dic withSignDic:signDic];
    NSData *postData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:@"http://120.78.157.132:82/index.php/lend/index?api=api1002"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    request.HTTPMethod = @"POST";
    //设置请求参数
    request.HTTPBody = postData;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        [self hiddenProgressHUD];
        if (connectionError) {
            if (connectionError.code == -1001) {
                [self.view makeToast:@"请求网络超时，请稍后再试" duration:1 position:CSToastPositionCenter];
            }
        }else{
            NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];//data解析为dictionary
            if (!dictionary) {
                NSLog(@"数据缺失");
                return;
            }
            NSString * compareMD5Str = [md5Code encodeStringWithDictionary:dictionary];
            NSString * signResult = [dictionary objectForKey:@"sign"];//接到的json串sign字段
            NSDictionary * jsonData = [md5Code getNSDictionaryData:dictionary];//解析为字典数据
            if ([jsonData[@"code"] isEqualToString:@"000000"]) {
                if ([jsonData[@"body"] count] > 0) {
                    if ([compareMD5Str isEqualToString:signResult]) {
                        self.classifyCellArr = jsonData[@"body"];
                        [self.classifyChooseView layoutWithArray:self.classifyCellArr withButton:self.classifyBtn];
                        [self.tableView reloadData];
                        NSLog(@"成功");
                    }else{
                        NSLog(@"失败");
                        ShowMessage(jsonData[@"msg"]);
                    }
                }else{
                    ShowMessage(jsonData[@"msg"]);
                }
            }else{
                ShowMessage(jsonData[@"msg"]);
            }
        }
    }];
}
//状态栏
- (void)statusMenuList{
    [self showprogressHUD];
    NSString * timeStr = [self currentTimeStr];
    //str
    NSDictionary * dic = @{
                           @"userName":[UserModel sharedInstanced].mobile,
                           @"bigtitle":self.bigtitle,
                           @"num":@"1",
                           @"t":timeStr
                           };
    //sign
    NSDictionary * signDic = @{
                               @"userName":[UserModel sharedInstanced].mobile,
                               @"bigtitle":self.bigtitle,
                               @"num":@"1",
                               @"t":timeStr,
                               @"merchantNo":@"80d709a97af1b8ab"
                               };
    SortMD5Code * md5Code = [[SortMD5Code alloc] init];
    NSString * jsonStr = [md5Code getJsonWithStrDic:dic withSignDic:signDic];
    NSData *postData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:@"http://120.78.157.132:82/index.php/lend/index?api=api1002"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    request.HTTPMethod = @"POST";
    //设置请求参数
    request.HTTPBody = postData;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        [self hiddenProgressHUD];
        if (connectionError) {
            if (connectionError.code == -1001) {
                [self.view makeToast:@"请求网络超时，请稍后再试" duration:1 position:CSToastPositionCenter];
            }
        }else{
            NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];//data解析为dictionary
            if (!dictionary) {
                NSLog(@"数据缺失");
                return;
            }
            NSString * compareMD5Str = [md5Code encodeStringWithDictionary:dictionary];
            NSString * signResult = [dictionary objectForKey:@"sign"];//接到的json串sign字段
            NSDictionary * jsonData = [md5Code getNSDictionaryData:dictionary];//解析为字典数据
            if ([jsonData[@"code"] isEqualToString:@"000000"]) {
                if ([jsonData[@"body"] count] > 0) {
                    if ([compareMD5Str isEqualToString:signResult]) {
                        self.statusCellArr = jsonData[@"body"];
                        [self.statusChooseView layoutWithArray:self.statusCellArr withButton:self.statusBtn];
                        [self.tableView reloadData];
                        NSLog(@"成功");
                    }else{
                        NSLog(@"失败");
                        ShowMessage(jsonData[@"msg"]);
                    }
                }else{
                    ShowMessage(jsonData[@"msg"]);
                }
            }else{
                ShowMessage(jsonData[@"msg"]);
            }
        }
    }];
}
//期限栏
- (void)timeMenuList{
    [self showprogressHUD];
    NSString * timeStr = [self currentTimeStr];
    //str
    NSDictionary * dic = @{
                           @"userName":[UserModel sharedInstanced].mobile,
                           @"bigtitle":self.bigtitle,
                           @"num":@"2",
                           @"t":timeStr
                           };
    //sign
    NSDictionary * signDic = @{
                               @"userName":[UserModel sharedInstanced].mobile,
                               @"bigtitle":self.bigtitle,
                               @"num":@"2",
                               @"t":timeStr,
                               @"merchantNo":@"80d709a97af1b8ab"
                               };
    SortMD5Code * md5Code = [[SortMD5Code alloc] init];
    NSString * jsonStr = [md5Code getJsonWithStrDic:dic withSignDic:signDic];
    NSData *postData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:@"http://120.78.157.132:82/index.php/lend/index?api=api1002"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    request.HTTPMethod = @"POST";
    //设置请求参数
    request.HTTPBody = postData;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        [self hiddenProgressHUD];
        if (connectionError) {
            if (connectionError.code == -1001) {
                [self.view makeToast:@"请求网络超时，请稍后再试" duration:1 position:CSToastPositionCenter];
            }
        }else{
            NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];//data解析为dictionary
            if (!dictionary) {
                NSLog(@"数据缺失");
                return;
            }
            NSString * compareMD5Str = [md5Code encodeStringWithDictionary:dictionary];
            NSString * signResult = [dictionary objectForKey:@"sign"];//接到的json串sign字段
            NSDictionary * jsonData = [md5Code getNSDictionaryData:dictionary];//解析为字典数据
            if ([jsonData[@"code"] isEqualToString:@"000000"]) {
                if ([jsonData[@"body"] count] > 0) {
                    if ([compareMD5Str isEqualToString:signResult]) {
                        self.timeCellArr = jsonData[@"body"];
                        [self.timeChooseView layoutWithArray:self.timeCellArr withButton:self.timeBtn];
                        [self.tableView reloadData];
                        NSLog(@"成功");
                    }else{
                        NSLog(@"失败");
                        ShowMessage(jsonData[@"msg"]);
                    }
                }else{
                    ShowMessage(jsonData[@"msg"]);
                }
            }else{
                ShowMessage(jsonData[@"msg"]);
            }
        }
    }];
}
//借率栏
- (void)rateMenuList{
    [self showprogressHUD];
    NSString * timeStr = [self currentTimeStr];
    //str
    NSDictionary * dic = @{
                           @"userName":[UserModel sharedInstanced].mobile,
                           @"bigtitle":self.bigtitle,
                           @"num":@"3",
                           @"t":timeStr
                           };
    //sign
    NSDictionary * signDic = @{
                               @"userName":[UserModel sharedInstanced].mobile,
                               @"bigtitle":self.bigtitle,
                               @"num":@"3",
                               @"t":timeStr,
                               @"merchantNo":@"80d709a97af1b8ab"
                               };
    SortMD5Code * md5Code = [[SortMD5Code alloc] init];
    NSString * jsonStr = [md5Code getJsonWithStrDic:dic withSignDic:signDic];
    NSData *postData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:@"http://120.78.157.132:82/index.php/lend/index?api=api1002"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    request.HTTPMethod = @"POST";
    //设置请求参数
    request.HTTPBody = postData;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        [self hiddenProgressHUD];
        if (connectionError) {
            if (connectionError.code == -1001) {
                [self.view makeToast:@"请求网络超时，请稍后再试" duration:1 position:CSToastPositionCenter];
            }
        }else{
            NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];//data解析为dictionary
            if (!dictionary) {
                NSLog(@"数据缺失");
                return;
            }
            NSString * compareMD5Str = [md5Code encodeStringWithDictionary:dictionary];
            NSString * signResult = [dictionary objectForKey:@"sign"];//接到的json串sign字段
            NSDictionary * jsonData = [md5Code getNSDictionaryData:dictionary];//解析为字典数据
            if ([jsonData[@"code"] isEqualToString:@"000000"]) {
                if ([jsonData[@"body"] count] > 0) {
                    if ([compareMD5Str isEqualToString:signResult]) {
                        self.rateCellArr = jsonData[@"body"];
                        [self.rateChooseView layoutWithArray:self.rateCellArr withButton:self.rateBtn];
                        [self.tableView reloadData];
                        NSLog(@"成功");
                    }else{
                        NSLog(@"失败");
                        ShowMessage(jsonData[@"msg"]);
                    }
                }else{
                    ShowMessage(jsonData[@"msg"]);
                }
            }else{
                ShowMessage(jsonData[@"msg"]);
            }
        }
    }];
}
//点击后显示的数据
- (void)showDataMenuList{
    [self showprogressHUD];
    NSString * timeStr = [self currentTimeStr];
    //str
    NSDictionary * dic = @{
                           @"userName":[UserModel sharedInstanced].mobile,
                           @"bigtitle":self.bigtitle,
                           @"t":timeStr,
                           @"tid":self.classifyContentStr,
                           @"zname":self.statusContentStr,
                           @"qid":self.timeContentStr,
                           @"jid":self.rateContentStr,
                           @"page":@(self.currentPage)
                           };
    //sign
    NSDictionary * signDic = @{
                               @"userName":[UserModel sharedInstanced].mobile,
                               @"bigtitle":self.bigtitle,
                               @"t":timeStr,
                               @"tid":self.classifyContentStr,
                               @"zname":self.statusContentStr,
                               @"qid":self.timeContentStr,
                               @"jid":self.rateContentStr,
                               @"page":@(self.currentPage),
                               @"merchantNo":@"80d709a97af1b8ab"
                               };
    SortMD5Code * md5Code = [[SortMD5Code alloc] init];
    NSString * jsonStr = [md5Code getJsonWithStrDic:dic withSignDic:signDic];
    NSData *postData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:@"http://120.78.157.132:82/index.php/lend/index?api=api1004"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方式
    request.HTTPMethod = @"POST";
    //设置请求参数
    request.HTTPBody = postData;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        [self hiddenProgressHUD];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (connectionError) {
            if (connectionError.code == -1001) {
                [self.view makeToast:@"请求网络超时，请稍后再试" duration:1 position:CSToastPositionCenter];
            }
        }else{
            NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];//data解析为dictionary
            if (!dictionary) {
                NSLog(@"数据缺失");
                return;
            }
            NSString * compareMD5Str = [md5Code encodeStringWithDictionary:dictionary];
            NSString * signResult = [dictionary objectForKey:@"sign"];//接到的json串sign字段
            NSDictionary * jsonData = [md5Code getNSDictionaryData:dictionary];//解析为字典数据
            if ([jsonData[@"code"] isEqualToString:@"000000"]) {
                if ([jsonData[@"body"] count] > 0) {
                    if ([compareMD5Str isEqualToString:signResult]) {
                        if (self.currentPage == 1) {
                            [self.cellDataArr removeAllObjects];
                        }
                        //            self.cellDataArr = jsonData[@"body"];
                        [self.cellDataArr addObjectsFromArray:jsonData[@"body"]];
                        [self.tableView reloadData];
                        NSLog(@"成功");
                    }else{
                        NSLog(@"失败");
                        ShowMessage(jsonData[@"msg"]);
                        [self.cellDataArr removeAllObjects];
                        [self.tableView reloadData];
                    }
                }else{
                    ShowMessage(jsonData[@"msg"]);
                }
            }else{
                ShowMessage(jsonData[@"msg"]);
            }
        }
    }];
}
@end
@implementation LendViewController

- (instancetype)init{
    if (self = [super init]) {
        //添加观察者
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getFirstTitleAndSecondTitle:) name:@"getFirstTitleAndSecondTitle" object:nil];
        self.view.backgroundColor = [UIColor whiteColor];
        
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setStatusBarBackgroundColor:[UIColor whiteColor]];
    self.title = @"出借";
    
    self.classifyContentStr = @"";
    self.bigtitle = @"0";
//    //添加观察者
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getFirstTitleAndSecondTitle:) name:@"getFirstTitleAndSecondTitle" object:nil];
    self.currentPage = 1;
    self.statusContentStr = @"";
    self.timeContentStr = @"";
    self.rateContentStr = @"";
//    //网络,下拉菜单接口
//    [self classifyMenuList];
//    [self statusMenuList];
//    [self timeMenuList];
//    [self rateMenuList];
    //tableView数据
//    [self showDataMenuList];
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //构造导航栏
    [self initNavigate];
    //构造产品分类背景
    [self initProDuceClassify];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
    //下拉菜单
    __weak typeof (self)weakSelf = self;
    self.classifyChooseView = [[ClassifyChooseView alloc] initWithFrame:CGRectMake(0, NAVI_HEIGHT + self.viewBar.bottom, SCREEN_WIDTH, SCREEN_HEIGHT - self.viewBar.height - NAVI_HEIGHT - TABBAR_HEIGHT)];
    self.statusChooseView = [[StatusChooseView alloc] initWithFrame:CGRectMake(0, NAVI_HEIGHT + self.viewBar.bottom, SCREEN_WIDTH, SCREEN_HEIGHT - self.viewBar.height - NAVI_HEIGHT - TABBAR_HEIGHT)];
    self.timeChooseView = [[TimeChooseView alloc] initWithFrame:CGRectMake(0, NAVI_HEIGHT + self.viewBar.bottom, SCREEN_WIDTH, SCREEN_HEIGHT - self.viewBar.height - NAVI_HEIGHT - TABBAR_HEIGHT)];
    self.rateChooseView = [[RateChooseView alloc] initWithFrame:CGRectMake(0, NAVI_HEIGHT + self.viewBar.bottom, SCREEN_WIDTH, SCREEN_HEIGHT - self.viewBar.height - NAVI_HEIGHT - TABBAR_HEIGHT)];
    self.classifyChooseView.classifyChooseViewBlock = ^(NSString * btnStr, NSString * classifyID) {
        weakSelf.currentPage = 1;
        [weakSelf.classifyBtn setTitle:[NSString stringWithFormat:@"%@", btnStr] forState:UIControlStateNormal];
        [weakSelf.classifyBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        weakSelf.classifyContentStr = classifyID;
        [weakSelf showDataMenuList];//点击查看数据
    };
    self.statusChooseView.statusChooseViewBlock = ^(NSString * btnStr, NSString * statusID) {
        weakSelf.currentPage = 1;
        [weakSelf.statusBtn setTitle:[NSString stringWithFormat:@"%@", btnStr] forState:UIControlStateNormal];
        [weakSelf.statusBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        weakSelf.statusContentStr = btnStr;
        [weakSelf showDataMenuList];//点击查看数据
    };
    self.timeChooseView.timeChooseViewBlock = ^(NSString * btnStr, NSString * timeID) {
        weakSelf.currentPage = 1;
        [weakSelf.timeBtn setTitle:[NSString stringWithFormat:@"%@", btnStr] forState:UIControlStateNormal];
        [weakSelf.timeBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        weakSelf.timeContentStr = timeID;
        [weakSelf showDataMenuList];//点击查看数据
    };
    self.rateChooseView.rateChooseViewBlock = ^(NSString * btnStr, NSString * rateID) {
        weakSelf.currentPage = 1;
        [weakSelf.rateBtn setTitle:[NSString stringWithFormat:@"%@", btnStr] forState:UIControlStateNormal];
        [weakSelf.rateBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        weakSelf.rateContentStr = rateID;
        [weakSelf showDataMenuList];//点击查看数据
    };
    
//    self.classifyContentStr = @"";
//    self.bigtitle = @"0";
//
//    self.currentPage = 1;
//    self.statusContentStr = @"";
//    self.timeContentStr = @"";
//    self.rateContentStr = @"";
//    //网络,下拉菜单接口
//    [self classifyMenuList];
//    [self statusMenuList];
//    [self timeMenuList];
//    [self rateMenuList];
//    //tableView数据
//    [self showDataMenuList];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.classifyBtn.selected = NO;
    self.statusBtn.selected = NO;
    self.timeBtn.selected = NO;
    self.rateBtn.selected = NO;
    if (self.classifyChooseView) {
        [self.classifyChooseView cancelBtnClick];
        [self.statusChooseView cancelBtnClick];
        [self.timeChooseView cancelBtnClick];
        [self.rateChooseView cancelBtnClick];
    }
}
#pragma mark - init
//构造导航栏
- (void)initNavigate{
    self.navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 30/WIDTH_6_SCALE, 30/WIDTH_6_SCALE)];
    //福乐精选
    self.siftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.siftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.siftBtn setTitleColor:getColor(@"ff8819") forState:UIControlStateSelected];
    self.siftBtn.frame = CGRectMake(0, 0, btnW, 20/WIDTH_6_SCALE);
    self.siftBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [self.siftBtn addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.siftBtn setTitle:@"福乐精选" forState:UIControlStateNormal];
    self.siftBtn.selected = YES;
    [self.navView addSubview:self.siftBtn];
    //福乐月息
    self.interestBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.interestBtn.frame = CGRectMake(self.siftBtn.right, self.siftBtn.top, self.siftBtn.width, self.siftBtn.height);
    [self.interestBtn setTitleColor:getColor(@"ff8819") forState:UIControlStateSelected];
    [self.interestBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.interestBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [self.interestBtn addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.interestBtn setTitle:@"福乐月息" forState:UIControlStateNormal];
    [self.navView addSubview:self.interestBtn];
    //福乐白金
    self.platinumBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.platinumBtn.frame = CGRectMake(self.interestBtn.right, self.siftBtn.top, self.siftBtn.width, self.siftBtn.height);
    [self.platinumBtn setTitleColor:getColor(@"ff8819") forState:UIControlStateSelected];
    [self.platinumBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.platinumBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [self.platinumBtn addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.platinumBtn setTitle:@"福乐白金" forState:UIControlStateNormal];
    [self.navView addSubview:self.platinumBtn];
    //下面滑动条
    self.sliderLabel = [[UILabel alloc]initWithFrame:CGRectMake(btnX, self.navView.bottom - 5/WIDTH_6_SCALE, 45/WIDTH_6_SCALE, 2/WIDTH_6_SCALE)];
    self.sliderLabel.backgroundColor = getColor(@"ff8819");
    [self.navView addSubview:self.sliderLabel];
    
    self.navigationItem.titleView = self.navView;
}
//构造产品分类
- (void)initProDuceClassify{
    //背景
    UIView * viewBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44/WIDTH_6_SCALE)];
    viewBar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:viewBar];
    //按钮背景
    UIView * btnBGView = [[UIView alloc] initWithFrame:CGRectMake(18/WIDTH_6_SCALE, 0, SCREEN_WIDTH - 36/WIDTH_6_SCALE, viewBar.height)];
    btnBGView.backgroundColor = [UIColor clearColor];
    [viewBar addSubview:btnBGView];
    //底部分割线
    UIView * bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, viewBar.height - 1/WIDTH_6_SCALE, SCREEN_WIDTH, 1/WIDTH_6_SCALE)];
    bottomLineView.backgroundColor = getColor(grayColor);
    [viewBar addSubview:bottomLineView];
    //类型分别按钮
    int classifyBtnW = (SCREEN_WIDTH - 36/WIDTH_6_SCALE)/4;
    int classifyBtnY = 12/WIDTH_6_SCALE;
    int classifyBtnH = viewBar.height - 2*classifyBtnY;
    for (int i = 0; i<4; i++) {
        UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(classifyBtnW*i, classifyBtnY, classifyBtnW, classifyBtnH)];
        btn.tag = 2000+i;
        btn.titleLabel.font = DEF_FontSize_12;
//        [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        [btn setTitleColor:getColor(@"767676") forState:UIControlStateNormal];
        [btnBGView addSubview:btn];
    }
    //分类按钮
    UIButton * classifyBtn = (UIButton*)[self.view viewWithTag:2000];
    [classifyBtn setTitle:@"分类" forState:UIControlStateNormal];
    [classifyBtn addTarget:self action:@selector(chooseFirstBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.classifyBtn = classifyBtn;
    //状态按钮
    UIButton * statusBtn = (UIButton *)[self.view viewWithTag:2001];
    [statusBtn setTitle:@"状态" forState:UIControlStateNormal];
    [statusBtn addTarget:self action:@selector(chooseSecondBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.statusBtn = statusBtn;
    //期限按钮
    UIButton * timeBtn = (UIButton *)[self.view viewWithTag:2002];
    [timeBtn setTitle:@"期限" forState:UIControlStateNormal];
    [timeBtn addTarget:self action:@selector(chooseThirdBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.timeBtn = timeBtn;
    //借率按钮
    UIButton * rateBtn = (UIButton *)[self.view viewWithTag:2003];
    [rateBtn setTitle:@"借率" forState:UIControlStateNormal];
    [rateBtn addTarget:self action:@selector(chooseFourthBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.rateBtn = rateBtn;
    
    self.viewBar = viewBar;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.viewBar.bottom, SCREEN_WIDTH, SCREEN_HEIGHT - NAVI_HEIGHT - TABBAR_HEIGHT - self.viewBar.bottom) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[LendTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LendTableViewCell class])];
        __weak typeof (self)weakSelf = self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.currentPage = 1;
            [self showDataMenuList];
        }];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            weakSelf.currentPage ++;
            [self showDataMenuList];
        }];
    }return _tableView;
}

- (NSMutableArray *)classifyCellArr{
    if (!_classifyCellArr) {
        _classifyCellArr = @[].mutableCopy;
    }return _classifyCellArr;
}
- (NSMutableArray *)statusCellArr{
    if (!_statusCellArr) {
        _statusCellArr = @[].mutableCopy;
    }return _statusCellArr;
}
- (NSMutableArray *)timeCellArr{
    if (!_timeCellArr) {
        _timeCellArr = @[].mutableCopy;
    }return _timeCellArr;
}
- (NSMutableArray *)rateCellArr{
    if (!_rateCellArr) {
        _rateCellArr = @[].mutableCopy;
    }return _rateCellArr;
}
- (NSMutableArray *)cellDataArr{
    if (!_cellDataArr) {
        _cellDataArr = @[].mutableCopy;
    }return _cellDataArr;
}
#pragma mark - action
- (void)getFirstTitleAndSecondTitle:(NSNotification *)notification{
    NSDictionary * dic = notification.userInfo;
    self.bigtitle = dic[@"firstTitleID"];
    if ([self.bigtitle isEqualToString:@"1"]) {
        self.sliderLabel.frame = CGRectMake(self.siftBtn.left + (btnW - sliderLabelW)/2, self.navView.bottom - 5/WIDTH_6_SCALE, sliderLabelW, 2/WIDTH_6_SCALE);
        self.siftBtn.selected = YES;
        self.interestBtn.selected = NO;
        self.platinumBtn.selected = NO;
    }else if ([self.bigtitle isEqualToString:@"2"]){
        self.sliderLabel.frame = CGRectMake(self.interestBtn.left + (btnW - sliderLabelW)/2, self.navView.bottom - 5/WIDTH_6_SCALE, sliderLabelW, 2/WIDTH_6_SCALE);
        self.siftBtn.selected = NO;
        self.interestBtn.selected = YES;
        self.platinumBtn.selected = NO;
    }else{
        self.sliderLabel.frame = CGRectMake(self.platinumBtn.left + (btnW - sliderLabelW)/2, self.navView.bottom - 5/WIDTH_6_SCALE, sliderLabelW, 2/WIDTH_6_SCALE);
        self.siftBtn.selected = NO;
        self.interestBtn.selected = NO;
        self.platinumBtn.selected = YES;
    }
    self.classifyContentStr = dic[@"secondTitleID"];
    [self.classifyBtn setTitle:dic[@"secondTitle"] forState:UIControlStateNormal];
    [self.classifyBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    
    
    self.currentPage = 1;
    self.statusContentStr = @"";
    self.timeContentStr = @"";
    self.rateContentStr = @"";
    //网络,下拉菜单接口
    [self classifyMenuList];
    [self statusMenuList];
    [self timeMenuList];
    [self rateMenuList];
    //tableView数据
    [self showDataMenuList];
}
- (void)sliderAction:(UIButton *)sender{
    self.classifyContentStr = @"";
    self.statusContentStr = @"";
    self.timeContentStr = @"";
    self.rateContentStr = @"";
    self.classifyBtn.selected = NO;
    self.statusBtn.selected = NO;
    self.timeBtn.selected = NO;
    self.rateBtn.selected = NO;
    [self.classifyBtn setTitle:@"分类" forState:UIControlStateNormal];
    [self.classifyBtn setTitleColor:getColor(@"767676") forState:UIControlStateNormal];
    [self.statusBtn setTitle:@"状态" forState:UIControlStateNormal];
    [self.statusBtn setTitleColor:getColor(@"767676") forState:UIControlStateNormal];
    [self.timeBtn setTitle:@"期限" forState:UIControlStateNormal];
    [self.timeBtn setTitleColor:getColor(@"767676") forState:UIControlStateNormal];
    [self.rateBtn setTitle:@"借率" forState:UIControlStateNormal];
    [self.rateBtn setTitleColor:getColor(@"767676") forState:UIControlStateNormal];
    
    if (sender.centerX<sender.width) {
        self.bigtitle = @"0";
        [UIView animateWithDuration:0.5 animations:^{
            self.sliderLabel.frame = CGRectMake(sender.left + (btnW - sliderLabelW)/2, self.navView.bottom - 5/WIDTH_6_SCALE, sliderLabelW, 2/WIDTH_6_SCALE);
            self.siftBtn.selected = YES;
            self.interestBtn.selected = NO;
            self.platinumBtn.selected = NO;
        }];
        [self showDataMenuList];
        //网络,下拉菜单接口
        [self classifyMenuList];
        [self statusMenuList];
        [self timeMenuList];
        [self rateMenuList];
    }else if (sender.centerX<2*sender.width){
        self.bigtitle = @"1";
        [UIView animateWithDuration:0.5 animations:^{
            self.sliderLabel.frame = CGRectMake(sender.left + (btnW - sliderLabelW)/2, self.navView.bottom - 5/WIDTH_6_SCALE, sliderLabelW, 2/WIDTH_6_SCALE);
            self.siftBtn.selected = NO;
            self.interestBtn.selected = YES;
            self.platinumBtn.selected = NO;
        }];
        [self showDataMenuList];
        //网络,下拉菜单接口
        [self classifyMenuList];
        [self statusMenuList];
        [self timeMenuList];
        [self rateMenuList];
    }else{
#pragma mark - 判断是否为福乐白金
        self.bigtitle = @"2";
        NoPlatinumMemberView * noPlatinumMenberView = [[NoPlatinumMemberView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [noPlatinumMenberView showView];
        
        [UIView animateWithDuration:0.5 animations:^{
            self.sliderLabel.frame = CGRectMake(sender.left + (btnW - sliderLabelW)/2, self.navView.bottom - 5/WIDTH_6_SCALE, sliderLabelW, 2/WIDTH_6_SCALE);
            self.siftBtn.selected = NO;
            self.interestBtn.selected = NO;
            self.platinumBtn.selected = YES;
        }];
        [self showDataMenuList];
        //网络,下拉菜单接口
        [self classifyMenuList];
        [self statusMenuList];
        [self timeMenuList];
        [self rateMenuList];
    }
}
- (void)chooseFirstBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
//        [self.statusChooseView cancelBtnClick];
//        [self.timeChooseView cancelBtnClick];
//        [self.rateChooseView cancelBtnClick];
//        [self.classifyChooseView showView];
        
        dispatch_async(dispatch_queue_create(0, 0), ^{
//            [NSThread sleepForTimeInterval:2];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.statusChooseView cancelBtnClick];
                [self.timeChooseView cancelBtnClick];
                [self.rateChooseView cancelBtnClick];
            });
            [NSThread sleepForTimeInterval:0.2];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.classifyChooseView showView];
            });
        });
        
    }else{
        [self.classifyChooseView cancelBtnClick];
    }
}

- (void)chooseSecondBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
//        [self.classifyChooseView cancelBtnClick];
//        [self.timeChooseView cancelBtnClick];
//        [self.rateChooseView cancelBtnClick];
//        [self.statusChooseView showView];
        
        dispatch_async(dispatch_queue_create(0, 0), ^{
//            [NSThread sleepForTimeInterval:2];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.classifyChooseView cancelBtnClick];
                [self.timeChooseView cancelBtnClick];
                [self.rateChooseView cancelBtnClick];
            });
            [NSThread sleepForTimeInterval:0.2];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.statusChooseView showView];
            });
        });
    }else{
        [self.statusChooseView cancelBtnClick];
    }
}

- (void)chooseThirdBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
//        [self.classifyChooseView cancelBtnClick];
//        [self.statusChooseView cancelBtnClick];
//        [self.rateChooseView cancelBtnClick];
//        [self.timeChooseView showView];
        
        dispatch_async(dispatch_queue_create(0, 0), ^{
//            [NSThread sleepForTimeInterval:2];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.classifyChooseView cancelBtnClick];
                [self.statusChooseView cancelBtnClick];
                [self.rateChooseView cancelBtnClick];
            });
            [NSThread sleepForTimeInterval:0.2];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.timeChooseView showView];
            });
        });
    }else{
        [self.timeChooseView cancelBtnClick];
    }
}

- (void)chooseFourthBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
//        [self.classifyChooseView cancelBtnClick];
//        [self.statusChooseView cancelBtnClick];
//        [self.timeChooseView cancelBtnClick];
//        [self.rateChooseView showView];
        
        dispatch_async(dispatch_queue_create(0, 0), ^{
//            [NSThread sleepForTimeInterval:2];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.classifyChooseView cancelBtnClick];
                [self.statusChooseView cancelBtnClick];
                [self.timeChooseView cancelBtnClick];
            });
            [NSThread sleepForTimeInterval:0.2];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.rateChooseView showView];
            });
        });
    }else{
        [self.rateChooseView cancelBtnClick];
    }
}

- (void)ifShutBtnClick:(UIButton *)sender{
    CloseCourseView * closeCourseView = [[CloseCourseView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    closeCourseView.sureBtnClickBlock = ^(){
        [self.tableView reloadData];
    };
    [closeCourseView showView];
}
#pragma mark - tableViewDelegate和tableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LendTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LendTableViewCell class]) forIndexPath:indexPath];
//    cell.model = self.cellDataArr[indexPath.row];
    cell.siftPageBlock = ^(NSString * bidID){
        SiftDetailViewController * siftDetailVC = [[SiftDetailViewController alloc] init];
        siftDetailVC.hidesBottomBarWhenPushed = YES;
        siftDetailVC.bidID = bidID;
        [self.navigationController pushViewController:siftDetailVC animated:YES];
    };
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
//    return self.cellDataArr.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110/WIDTH_6_SCALE;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * sectionView = [[UIView alloc] init];
    sectionView.backgroundColor = [UIColor whiteColor];
    //上分割线
    UIView * topLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1/WIDTH_6_SCALE)];
    topLineView.backgroundColor = getColor(grayColor);
    [sectionView addSubview:topLineView];
    //下分割线
    UIView * bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, sectionH - 1/WIDTH_6_SCALE, SCREEN_WIDTH, 1/WIDTH_6_SCALE)];
    bottomLineView.backgroundColor = getColor(grayColor);
    [sectionView addSubview:bottomLineView];
    //关闭按钮
    UIButton * ifShutBtn = [[UIButton alloc] init];
    [sectionView addSubview:ifShutBtn];
    [ifShutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(sectionView.mas_right).mas_offset(-18/WIDTH_6_SCALE);
        make.top.equalTo(sectionView.mas_top).mas_offset(9/WIDTH_6_SCALE);
        make.bottom.equalTo(sectionView.mas_bottom).mas_offset(-8/WIDTH_6_SCALE);
        make.width.mas_offset(36/WIDTH_6_SCALE);
    }];
    ifShutBtn.layer.cornerRadius = 9;
    ifShutBtn.layer.masksToBounds = YES;
    [ifShutBtn setBackgroundColor:getColor(@"f0f0f0")];
    [ifShutBtn addTarget:self action:@selector(ifShutBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [ifShutBtn setTitle:@"关闭" forState:UIControlStateNormal];
    [ifShutBtn setTitleColor:getColor(@"bdbdbd") forState:UIControlStateNormal];
    ifShutBtn.titleLabel.font = DEF_FontSize_10;
    //出借教程图标
    UIImageView * lendIconImg = [[UIImageView alloc] init];
    lendIconImg.backgroundColor = [UIColor redColor];
    [sectionView addSubview:lendIconImg];
    [lendIconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sectionView.mas_left).mas_offset(30/WIDTH_6_SCALE);
        make.centerY.equalTo(sectionView.mas_centerY);
        make.width.mas_offset(11/WIDTH_6_SCALE);
        make.height.mas_offset(12/WIDTH_6_SCALE);
    }];
    //查看出借标题
    UILabel * checkTitleLab = [[UILabel alloc] init];
    [self label:checkTitleLab withFont:DEF_FontSize_10 withText:@"查看出借教程" withTextColor:getColor(@"8ca3bd") withTextAlignment:NSTextAlignmentLeft withView:sectionView];
    [checkTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lendIconImg.mas_right).mas_offset(10/WIDTH_6_SCALE);
        make.centerY.equalTo(lendIconImg.mas_centerY);
        make.height.mas_offset(14/WIDTH_6_SCALE);
    }];
    return sectionView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    if ([userDefault objectForKey:ifShutCourse]) {
        return 0;
    }else{
        return sectionH;
    }
}
#pragma mark - method
- (void)firstBtnState:(BOOL)firstState withSecondBtnState:(BOOL)secondState withThirdBtn:(BOOL)thirdState withFourthBtn:(BOOL)fourthState{
    self.classifyBtn.selected = firstState;
    self.statusBtn.selected = secondState;
    self.timeBtn.selected = thirdState;
    self.rateBtn.selected = fourthState;
}
@end
